package com.lelouch.bestpractice.mongorepository.controller;

import com.lelouch.bestpractice.mongorepository.domain.TestEntity;
import com.lelouch.bestpractice.mongorepository.domain.UserEntity;
import com.lelouch.bestpractice.mongorepository.service.TestEntityService;
import com.lelouch.bestpractice.mongorepository.service.UserEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统用户控制器
 * @author xukai
 * @date 20180807
 *
 */
@RestController
@RequestMapping("/user")
public class UserEntityController {


    @Autowired
    private UserEntityService userEntityService;

    @Autowired
    private TestEntityService testEntityService;

    @RequestMapping("/add")
    public UserEntity add(){
        UserEntity userEntity = new UserEntity();
        //userEntity.setId("1");
        userEntity.setUserName("lelouch");
        userEntity.setNo(1L);
       return userEntityService.add(userEntity);

    }

    @RequestMapping("/delete")
    public boolean delete(String id){
        return userEntityService.delete(id);
    }

    /**
     * update的时候带上id，save方法在没有id的时候默认添加，有id的时候是更新
     * @return
     */
    @RequestMapping("/update")
    public UserEntity update(){
        UserEntity userEntity = new UserEntity();
        userEntity.setId("5b8e4afd9f2e1c0900767afc");
        userEntity.setUserName("lelouch");
        userEntity.setNo(2L);
        userEntity.setEmail("565862329@qq.com");
        return userEntityService.update(userEntity);
    }

    @RequestMapping("/findById")
    public UserEntity findById(String id){
        return userEntityService.findById(id);
    }

    @RequestMapping("/findByUsername")
    public UserEntity findByUsername(@RequestParam("username") String username){
        return userEntityService.findByUsername(username);
    }




    @RequestMapping("/addTest")
    public TestEntity addTest(){
       TestEntity testEntity =new TestEntity();
        //userEntity.setId("1");
        testEntity.setName("test");
        return testEntityService.add(testEntity);

    }




}
